package com.game.autoTool.project.web.common;

import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class GlobalKeyListenerExample {
    public static void main(String[] args) {
        try {
            // 注册全局钩子
            GlobalScreen.registerNativeHook();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 添加键盘监听器
        GlobalScreen.addNativeKeyListener(new NativeKeyListener() {
            /**
             * 按下的时候不释放,会有多条记录,需要处理!!
             * @param e
             */


            @Override
            public void nativeKeyPressed(NativeKeyEvent e) {
                System.out.println("按下键: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
                System.out.println("按下键: " + e.getKeyCode());
            }

            @Override
            public void nativeKeyReleased(NativeKeyEvent e) {
                System.out.println("释放键: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
                System.out.println("释放键: " + e.getKeyCode());
            }

            @Override
            public void nativeKeyTyped(NativeKeyEvent e) {
                // 通常不需要处理
            }
        });
    }
}